package com.whmnx.chitchat.adapter;


import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.RecyclerView;


import com.classic.adapter.CommonRecyclerAdapter;
import com.tencent.qcloud.tim.uikit.modules.chat.layout.message.MessageListAdapter;
import com.tencent.qcloud.tim.uikit.modules.chat.layout.message.holder.MessageContentHolder;
import com.tencent.qcloud.tim.uikit.modules.chat.layout.message.holder.MessageCustomHolder;
import com.tencent.qcloud.tim.uikit.modules.message.MessageInfo;
import com.tencent.qcloud.tim.uikit.utils.BackgroundTasks;
import com.tencent.qcloud.tim.uikit.utils.TUIKitLog;
import com.whmnx.chitchat.adapter.holder.IOnCustomMessageDrawListener2;
import com.whmnx.chitchat.adapter.holder.MessageBaseHolder2;
import com.whmnx.chitchat.adapter.holder.MessageEmptyHolder2;
import com.whmnx.chitchat.adapter.holder.MessageHeaderHolder2;
import com.whmnx.chitchat.bean.ChatDetailsBean;
import com.whmnx.chitchat.constants.Constants;
import com.whmnx.chitchat.utils.SPUtil;
import com.whmnx.chitchat.view.MessageLayout;
import com.whmnx.chitchat.view.interfaces.IChatProvider;

import java.util.List;


/**
 * @author Administrator
 */
public class MessageListAdapter2 extends RecyclerView.Adapter {

    public static final int MSG_TYPE_HEADER_VIEW = -99;
    private static final String TAG = MessageListAdapter.class.getSimpleName();
    private boolean mLoading = false;
    private MessageLayout mRecycleView;
    private Context mContext;
    private List<ChatDetailsBean> mDataSource;
    private MessageLayout.OnItemClickListener mOnItemClickListener;
    private IOnCustomMessageDrawListener2 mOnCustomMessageDrawListener;
    //声明自定义的监听接口
    private OnRecyclerItemClickListener monItemClickListener;

    //提供set方法供Activity或Fragment调用
    public void setRecyclerItemClickListener(OnRecyclerItemClickListener listener){
        monItemClickListener=listener;
    }

    public MessageListAdapter2(Activity activity, List<ChatDetailsBean> mDataSource) {
        this.mContext = activity;
        this.mDataSource = mDataSource;
    }


    //然后再外面,参数中传入new ..
    public interface OnRecyclerItemClickListener{
        void onClick(View view,int pos);
    }


    public MessageLayout.OnItemClickListener getOnItemClickListener() {
        return this.mOnItemClickListener;
    }

    public void setOnItemClickListener(MessageLayout.OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    //用于创建ViewHolder
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = MessageEmptyHolder2.Factory.getInstance(parent, this, viewType);
        return holder;
    }

    //绑定数据
    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        final ChatDetailsBean msg = getItem(position);
        //向右边   自己的id
        if (null != msg) {
            String formAccount = SPUtil.getString(Constants.USERID);
            if (msg.getChatRecord_SendUserID().equals(formAccount)) {
                msg.setSelf(true);
            } else {
                msg.setSelf(false);
            }
        }
        MessageBaseHolder2 baseHolder = (MessageBaseHolder2) holder;
        baseHolder.setOnItemClickListener(mOnItemClickListener);
        switch (getItemViewType(position)) {
            case MSG_TYPE_HEADER_VIEW:
                ((MessageHeaderHolder2) baseHolder).setLoadingStatus(mLoading);
                break;
            case MessageInfo.MSG_TYPE_TEXT:
            case MessageInfo.MSG_TYPE_IMAGE:
            case MessageInfo.MSG_TYPE_FILE:
            case MessageInfo.MSG_TYPE_AUDIO:
                break;
            default:
                if (msg.getChatRecord_Type() < 4) {
                    TUIKitLog.e(TAG, "Never be here!");
                }
                break;
        }
        baseHolder.layoutViews(msg, position);
        // 对于自定义消息，需要在正常布局之后，交给外部调用者重新加载渲染
        if (getItemViewType(position) == MessageInfo.MSG_TYPE_CUSTOM) {
            MessageCustomHolder customHolder = (MessageCustomHolder) holder;
            if (mOnCustomMessageDrawListener != null) {
                mOnCustomMessageDrawListener.onDraw(customHolder, msg);
            }
        }

     /*  baseHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (monItemClickListener!=null){
                    monItemClickListener.onClick(v,position);
                }
            }
        });*/
    }

    public void setDataSource(IChatProvider provider) {
        if (provider == null) {
            mDataSource.clear();
        } else {
            mDataSource = provider.getDataSource();
            provider.setAdapter(this);
        }
        notifyDataSourceChanged(MessageLayout.DATA_CHANGE_TYPE_REFRESH, getItemCount());
    }


    @Override
    public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        mRecycleView = (MessageLayout) recyclerView;
    }

    public void showLoading() {
        if (mLoading) {
            return;
        }
        mLoading = true;
        notifyItemChanged(0);
    }

    @Override
    public void onViewRecycled(@NonNull RecyclerView.ViewHolder holder) {
        if (holder instanceof MessageContentHolder) {
            ((MessageContentHolder) holder).msgContentFrame.setBackground(null);
        }
    }

    public void notifyDataSourceChanged(final int type, final int value) {  //3， 0
        BackgroundTasks.getInstance().postDelayed(new Runnable() {
            @Override
            public void run() {
                mLoading = false;
                if (type == MessageLayout.DATA_CHANGE_TYPE_REFRESH) {
                    notifyDataSetChanged();
                    mRecycleView.scrollToEnd();
                } else if (type == MessageLayout.DATA_CHANGE_TYPE_ADD_BACK) {
                    notifyItemRangeInserted(mDataSource.size() + 1, value);
                  //  notifyDataSetChanged();
                    mRecycleView.scrollToEnd();
                } else if (type == MessageLayout.DATA_CHANGE_TYPE_UPDATE) {
                    notifyItemChanged(value);
                } else if (type == MessageLayout.DATA_CHANGE_TYPE_LOAD || type == MessageLayout.DATA_CHANGE_TYPE_ADD_FRONT) {
                    //加载条目为数0，只更新动画
                    if (value == 0) {
                        notifyItemChanged(0);
                    } else {
                        //加载过程中有可能之前第一条与新加载的最后一条的时间间隔不超过5分钟，时间条目需去掉，所以这里的刷新要多一个条目
                        if (getItemCount() > value) {
                            notifyItemRangeInserted(0, value);
                        } else {
                            notifyItemRangeInserted(0, value);
                        }
                    }
                } else if (type == MessageLayout.DATA_CHANGE_TYPE_DELETE) {
                    notifyItemRemoved(value + 1);
                    notifyDataSetChanged();
                    mRecycleView.scrollToEnd();
                }
            }
        }, 100);
    }

    //数据总数
    @Override
    public int getItemCount() {
        return mDataSource.size() + 1;
    }


   public List<ChatDetailsBean> getData() {
        return mDataSource;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return MSG_TYPE_HEADER_VIEW;
        }
        ChatDetailsBean msg = getItem(position);
      /*  if (null != msg){
            if (ChatDetailsBean.MSG_TYPE_TEXT == msg.getChatRecord_Type()){
                msg.setMsgType(ChatDetailsBean.MSG_TYPE_TEXT);
            }else if (ChatDetailsBean.MSG_TYPE_IMAGE == msg.getChatRecord_Type()){
                msg.setMsgType(ChatDetailsBean.MSG_TYPE_IMAGE);
            }else if (ChatDetailsBean.MSG_TYPE_AUDIO == msg.getChatRecord_Type()){
                msg.setMsgType(ChatDetailsBean.MSG_TYPE_AUDIO);
            }
        }else {
            return ChatDetailsBean.MSG_TYPE_TEXT;
        }*/
        return msg.getChatRecord_Type();
    }

  /*  public void setDataSource(IChatProvider provider) {
        if (provider == null) {
            mDataSource.clear();
        } else {
            mDataSource = provider.getDataSource();
          //  provider.setAdapter(this);
        }
        notifyDataSourceChanged(MessageLayout.DATA_CHANGE_TYPE_REFRESH, getItemCount());
    }*/

    public ChatDetailsBean getItem(int position) {
        if (position == 0 || mDataSource.size() == 0) {
            return null;
        }
        ChatDetailsBean info = mDataSource.get(position - 1);
        return info;
    }
}